package cn.codesea.housework_user.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.listener.OnCheckClickListener;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickUserListener;
import cn.codesea.housework_user.entity.User;

public class JHListRcvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<User> mDatas;

    private int mSelectedPos = 0;   //实现单选，保存当前选中的position

    private OnCheckClickListener listener;
    public void setOnCheckClickListener(OnCheckClickListener listener){
        this.listener = listener;
    }

    //声明自定义的监听接口
    private OnRecyclerItemClickUserListener monItemClickListener;

    //提供set方法供Activity或Fragment调用
    public void setRecyclerItemClickListener(OnRecyclerItemClickUserListener listener){
        monItemClickListener=listener;
    }

    public JHListRcvAdapter(Context context, List<User> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    public void setSelection(int position){
        this.mSelectedPos = position;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = mInflater.inflate(R.layout.item_recycler_jhlist, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemViewHolder){
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            User user = mDatas.get(position);
            itemViewHolder.name_tv.setText(user.getName());
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.name_tv)
        TextView name_tv;
        @BindView(R.id.cardView)
        CardView cardView;
        @BindView(R.id.checkbox)
        AppCompatCheckBox checkbox;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (monItemClickListener!=null){
                        monItemClickListener.onItemClick(getAdapterPosition(),mDatas);
                        if (checkbox.isChecked()){
                            checkbox.setChecked(false);
                        }else {
                            checkbox.setChecked(true);
                            mSelectedPos = getAdapterPosition();
                        }
                    }
                }
            });

            checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    listener.OnCheckClick(getAdapterPosition(),isChecked);
                }
            });
        }
    }


}
